<?php
class Auser {
	public static function main($funcode) {
		sview::verify();
		switch ($funcode) {
			case 1:
				self::__dodetails();
				break;
			case 2:
				self::__contributions();
				break;
			case 3:
				self::__dofollower();
				break;
			case 4:
				self::__dofollowing();
				break;
			case 5:
				self::__doeditinfo();
				break;
			case 6:
				self::__dofinance();
				break;
			case 7:
				self::__dounlocknick();
				break;
			default:
				sview::$view->assign_error(404, lang::text(404));
		}
	}
	
	//获取魅力资料信息
	private static function __charms($userid) {
		$data   = api_charms::get($userid);
		if ($data['dtoday']!= intval(config::$nowtime/24/3600)) {
			$data['dcharms']= 0;
		}
		$status     = 0;
		$lcharms    = api_charms::$lcharms;
		if ($data['charms'] >= $lcharms || intval($data['dcharms']) >= $lcharms) {
			$status = 1;
		}
		$data = array('charms'=>(int)$data['charms'], 'status'=>$status, 'timeout'=>(int)$data['airtime']);
		return $data;
	}
	
	//会员基础信息3-3-1
	private static function __dodetails() {
		$userid = isset(sview::$ctrl['userid'])? (int)sview::$ctrl['userid']:0;
		$ownerid= 0;
		$acl    = acl::getinst('vedio');
		if ($acl->user) {
			$ownerid = $acl->user['id'];
			if ($userid < 1) {
				$userid = $ownerid;
			}
		}
		$user   = model::load('live/userinfo')->set_devision($userid)->get_one($userid);
		if (!$user) {
			sview::$view->assign_error(80009, lang::text(80009));
		}
		$lv     = api_lv::item($user['lv'], 1);
		$rsexp  = 0;
		if ($user['lv'] > 1) {
			$plv   = api_lv::item($user['lv'] - 1, 1);
			$rsexp = $plv['coin'];
		}
		$following = api_stats::gattens($userid);
		$balance   = 0;
		if ($userid == $ownerid) {
			$finance = model::load('live/finance')->set_devision($userid)->get_one($userid);
			$balance = $finance['lastcoin'];
		}
		$user['sign']     = htmlspecialchars_decode($user['sign']);
		$user['nickname'] = htmlspecialchars_decode($user['nickname']);
		$user['city']     = htmlspecialchars_decode($user['city']);
		$birth  = empty($user['birth'])? strtotime('1991-01-01'):strtotime($user['birth']);
		$data   = array('userid'=>$user['userid'], 'nickname'=>$user['nickname'], 'avtar'=>$user['thumb'],
			'gender'=>$user['sex'], 'birthday'=>$birth, 'sign'=>$user['sign'], 'appbg'=>$user['appbg'], 'nickfee'=>api_nickuin::COIN,
			'rlv'=>$user['lv'], 'rlvicon'=>$lv['iconsrc'], 'rexp'=>$user['exp'], 'rsexp'=>$rsexp, 'reexp'=>$lv['coin'],
			'email'=>$user['email'], 'emailverify'=>$user['emailverify'], 'location'=>$user['city'], 'mobile'=>$user['mobile'],
			'following'=>$following, 'balance'=>$balance, 'isbirth'=>$user['isbirth'], 'isnick'=>$user['isnick'], 'roler'=>0);
		$roler  = 0;
		$anchor = model::load('live/anchor')->get_one($userid);
		$data['iscurated']= 0;
		if ($anchor) {
			$data['roler'] = 1;
			$data['alv']   = $user['lv'];
			$data['aexp']  = $user['exp'];
			$data['xfans'] = $anchor['xfans'];
			$data['charms']= $anchor['charms'];
			$lv     = api_lv::item($user['lv'], 0);
			$rsexp  = 0;
			if ($user['lv'] > 1) {
				$plv   = api_lv::item($user['lv'] - 1, 0);
				$rsexp = $plv['coin'];
			}
			$data['asexp']  = $rsexp;
			$data['aeexp']  = $lv['coin'];
			$data['alvicon']= $lv['iconsrc'];
			$data['isfollow'] = 0;
			if ($ownerid > 0 && $userid != $ownerid) {
				$data['isfollow'] = api_stats::hattens($ownerid, $userid);
			}
			$studio = model::load('live/studio')->get_item(array('userid'=>$userid));
			$data['studioid'] = empty($studio)? 0:$studio['id'];
			$data['iscurated']= api_studio::grade($anchor['starlv'], $studio['iscurated']);
			$data['isbanned'] = $studio['status'];
		}
		$data['mycharms'] = self::__charms($userid);
		sview::$view->assign_by_ref('datasets', $data);
		sview::$view->display();
	}
	
	//主播贡献榜列表3-3-2
	private static function __contributions() {
		$userid = (int)sview::$ctrl['userid'];
		$limit    = 10;
		$start    = 0;
		$list     = array();
		if ($userid > 0) {
			$where= array('anchorid'=>$userid, 'coin'=>array(0, orm::OP_GT));
			$data = model::load('live/fanspay')->set_devision($userid)->index_list(0, $limit, $where);
			if ($data) {
				$uorm = model::load('live/userinfo');
				foreach ($data as $item) {
					$user   = $uorm->set_devision($item['userid'])->get_one($item['userid']);
					$lv     = api_lv::item($user['lv'], 1);
					$list[] = array('userid'=>$user['userid'], 'nickname'=>htmlspecialchars_decode($user['nickname']), 'lv'=>$user['lv'],
						'lvicon'=>$lv['iconsrc'], 'avtar'=>$user['thumb'], 'coin'=>intval($item['coin']));
				}
			}
		}
		sview::$view->assign_by_ref('datasets', $list);
		sview::$view->display();
	}
	
	//我的/主播粉丝列表3-3-3
	private static function __dofollower() {
		$userid = (int)sview::$ctrl['userid'];
		$start  = (int)sview::$ctrl['offset'];
		$limit  = (int)sview::$ctrl['limit'];
		if ($limit < 1 || $limit > 30) {
			$limit = 15;
		}
		$acl    = acl::getinst('vedio');
		$ownerid= 0;
		if ($acl->user) {
			$ownerid = $acl->user['id'];
			if ($userid < 1) {
				$userid = $ownerid;
			}
		}
		$where  = array('userid'=>$userid, 'focus'=>0);
		$orm    = model::load('live/fans')->set_devision($userid);
		$userids= $orm->get_only_column($start, $limit, $where, 'fansid');
		$list   = array();
		if ($userids) {
			$aorm = model::load('live/anchor');
			$uorm = model::load('live/userinfo');
			foreach ($userids as $userid) {
				$user   = $uorm->set_devision($userid)->get_one($userid);
				if (!$user) {
					continue;
				}
				$status = 0;
				if ($ownerid > 0 && $ownerid != $user['userid']) {
					$status = api_stats::hattens($ownerid, $user['userid']);
				} else if ($ownerid > 0 && $ownerid == $user['userid']) {
					$status = 1;
				}
				$lv        = api_lv::item($user['lv'], 1);
				$anchor    = $aorm->get_one($user['userid']);
				$item      = array('userid'=>$user['userid'], 'nickname'=>$user['nickname'], 'lv'=>$user['lv'], 
					'exp'=>$user['exp'], 'avtar'=>$user['thumb'], 'sign'=>$user['sign'], 'attachs'=>$user['attachs']);
				$item['lvicon']   = $lv['iconsrc'];
				$item['nickname'] = htmlspecialchars_decode($item['nickname']);
				$item['sign']     = htmlspecialchars_decode($item['sign']);
				$item['roler']    = api_studio::isanchor($item['userid']);
				$item['isfollow'] = $status;
				$item['iscurated']= api_studio::grade($anchor['starlv'], 0);
				$list[]           = $item;
			}
		}
		sview::$view->assign_by_ref('datasets', $list);
		sview::$view->display();
	}
	
	//我/主播的关注列表3-3-4
	private static function __dofollowing() {
		$userid = (int)sview::$ctrl['userid'];
		$start  = (int)sview::$ctrl['offset'];
		$limit  = (int)sview::$ctrl['limit'];
		if ($limit < 1 || $limit > 30) {
			$limit = 15;
		}
		$acl    = acl::getinst('vedio');
		$ownerid= 0;
		if ($acl->user) {
			$ownerid = $acl->user['id'];
			if ($userid < 1) {
				$userid = $ownerid;
			}
		}
		$where  = array('userid'=>$userid, 'focus'=>1);
		$fansid = model::load('live/fans')->set_devision($userid)
			->get_only_column(0, -1, $where, 'fansid');
		$list   = array();
		if ($fansid) {
			$uorm = model::load('live/userinfo');
			$where= array(array('userid', $fansid, orm::OP_IN));
			$data = model::load('live/anchor')->get_only_list($start, $limit, $where, '*', 'exp', orm::DESC);
			foreach ($data as $item) {
				$status = 0;
				if ($ownerid > 0 && $ownerid != $userid) {
					$status = api_stats::hattens($ownerid, $item['userid']);
				} else if ($ownerid > 0 && $ownerid == $userid) {
					$status = 1;
				}
				$user  = $uorm->set_devision($item['userid'])->get_one($item['userid']);
				$lv    = api_lv::item($user['lv'], 0);
				$user['nickname'] = htmlspecialchars_decode($user['nickname']);
				$user['sign']     = htmlspecialchars_decode($user['sign']);
				$iscurated = api_studio::grade($item['starlv'], 0);
				$list[]= array('userid'=>$user['userid'], 'nickname'=>htmlspecialchars_decode($user['nickname']), 'lv'=>$user['lv'], 
					'lvicon'=>$lv['iconsrc'], 'avtar'=>$user['thumb'], 'sign'=>$user['sign'],
					'iscurated'=>$iscurated, 'isfollow'=>$status);
			}
		}
		sview::$view->assign_by_ref('datasets', $list);
		sview::$view->display();
	}
	
	//编辑会员信息3-3-5
	private static function __doeditinfo() {
		$acl = acl::getinst('vedio');
		if ($acl->user) {
			$userid = $acl->user['id'];
			$user   = array();
			$lang   = lang::load('filter.php');
			if (isset(sview::$ctrl['nickname'])) {
				$user['nickname'] = trim(sview::$ctrl['nickname']);
				if (!verify::is_nick($user['nickname'])) {
					sview::$view->assign_error(80002, lang::text(80002));
				}
				$user['nickname'] = htmlspecialchars($user['nickname']);
				//验证屏蔽关键词
				$res  = api_im::filter(2, $user['nickname']);
				if ($res && $res['ResultContent'] == 1) {
					sview::$view->assign_error(80002, lang::replace(80088, $lang['nickname']));
				}
				$nckuid  = api_nickuin::exist($user['nickname']);
				if ($nckuid > 0 && $userid != $nckuid) {
					sview::$view->assign_error(80033, lang::text(80033));
				}
				$musr = model::load('live/userinfo')->set_devision($userid)->get_one($userid);
				if ($musr['nickname'] != $user['nickname']) {
					if ($musr['isnick']) { //未付款的情况下
						sview::$view->assign_error(80033, lang::text(80033));
					}
					api_nickuin::delete($userid, $musr['nickname']);
					api_nickuin::insert($userid, $user['nickname']);
				}
				$user['isnick'] = 1;
				//更新studio表昵称
				$where  = array('userid'=>$userid);
				$sorm   = model::load('live/studio');
				$studio = $sorm->get_item($where);
				if ($studio) {
					$fields = array('author'=>$user['nickname']);
					$sorm->save($studio['id'], $fields);
				}
			}
			if (isset(sview::$ctrl['sign'])) {
				$user['sign'] = htmlspecialchars(trim(sview::$ctrl['sign']));
				//验证屏蔽关键词
				if ($user['sign']) {
					$res  = api_im::filter(2, $user['sign']);
					if ($res && $res['ResultContent'] == 1) {
						sview::$view->assign_error(60012, lang::replace(80088, $lang['sign']));
					}
				}
			}
			if (isset(sview::$ctrl['location'])) {
				$user['city'] = htmlspecialchars(trim(sview::$ctrl['location']));
				//验证屏蔽关键词
				if ($user['city']) {
					$res  = api_im::filter(2, $user['city']);
					if ($res && $res['ResultContent'] == 1) {
						sview::$view->assign_error(60010, lang::replace(80088, $lang['location']));
					}
				}
			}
			if (isset(sview::$ctrl['birthday'])) {
				$user['birth']   = is_numeric(sview::$ctrl['birthday'])?
					date('Y-m-d', sview::$ctrl['birthday']):sview::$ctrl['birthday'];
			}
			if (isset(sview::$ctrl['isbirth'])) {
				$user['isbirth'] = (sview::$ctrl['isbirth'] != 1)? 0:1;
			}
			if (isset(sview::$ctrl['gender'])) {
				$user['sex'] = intval(sview::$ctrl['gender']);
			}
			if (isset(sview::$ctrl['avtar']) && !empty(sview::$ctrl['avtar'])) {
				$user['thumb'] = trim(sview::$ctrl['avtar']);
			}
			if (isset(sview::$ctrl['appbg']) && !empty(sview::$ctrl['appbg'])) {
				$user['appbg'] = trim(sview::$ctrl['appbg']);
			}
			if ($user) {
				model::load('live/userinfo')->set_devision($userid)->save($userid, $user);
			}
		}
		sview::$view->display();
	}
	
	//会员昵称解锁3-3-7
	private static function __dounlocknick() {
		$acl    = acl::getinst('vedio');
		if (!$acl->user) {
			sview::$view->assign_error(508, lang::text(508));
		}
		$userid = $acl->user['id'];
		$user   = model::load('live/userinfo')->set_devision($userid)->get_one($userid);
		if ($user['isnick']) {
			//判断当前用户余额是否充足
			$coin  = api_nickuin::COIN;
			if(!api_exp::money($coin, 0, $userid)) {
				sview::$view->assign_error(80030, lang::text(80030));
			}
			api_exp::incr($coin, 0, 6, 0, $userid);
			//经验变化判断升级等 并发送广播通知
			api_lv::notify($userid, 0, 0);
			model::load('live/userinfo')->set_devision($userid)->save($userid, array('isnick'=>0));
		}
		sview::$view->display();
	}
	
	//获取财务信息3-3-6
	private static function __dofinance() {
		$acl = acl::getinst('vedio');
		$data= array('balance'=>0, 'income'=>0, 'bank'=>'****ABC', 'earning'=>0,
			'duration'=>0, 'starlv'=>'Star', 'incomemoney'=>0, 'start'=>0, 'end'=>0);
		if ($acl->user) {
			$userid  = $acl->user['id'];
			$finance = model::load('live/finance')->set_devision($userid)->get_one($userid);
			$data['balance'] = $finance['lastcoin'];
			$anchor  = model::load('live/anchor')->get_one($userid);
			if ($anchor) {
				$data['earning']     = $anchor['earningcoins'];
				$data['income']      = $anchor['earningcoins'] - $anchor['depositcoins'];
				$data['incomemoney'] = $data['income']/api_uc::STARRATIO;
				//获取提现的银行卡
				$where  = array('userid'=>$userid, 'inuse'=>1, 'status'=>1);
				$bank   = model::load('live/depositbank')->get_item($where);
				if ($bank) {
					$bank         = $bank['bank_account'];
					$data['bank'] = str_repeat('*', strlen($bank)-4).substr($bank, -4);
				}
				$star = model::load('live/star')->get_one($anchor['starlv']);
				($star) && ($data['starlv'] = $star['name']);
				if ($star['id'] == 12) {
					$res = self::FZairTime($userid);
				} else {
					$res = self::airTime($anchor['jointime'], $userid);
				}
				$data['duration'] = $res['duration'];
				$data['start']    = $res['start'];
				$data['end']      = $res['end'];
			}
		}
		sview::$view->assign_by_ref('datasets', $data);
		sview::$view->display();
	}
	
	//计算上个月的时间和本月的开播时间
	private static function airTime($jointime, $userid) {
		$mtime    = 30 * 86400;
		$month    = intval((config::$nowtime - $jointime) / $mtime);
		$orm      = model::load('live/stuevent')->set_devision($userid);
		if ($month < 1) {
			$where    = array('userid'=>$userid, array('offtime', $jointime, orm::OP_GE),
					array('offtime', config::$nowtime, orm::OP_LT));
			$data['duration'] = (int)$orm->get_total($where, 'SUM(lasttime - aritime)');
			$data['start'] = $jointime;
		} else {
			//计算本月的开播时间统计
			$start    = $jointime + $month * $mtime;
			$where    = array('userid'=>$userid, array('offtime', $start, orm::OP_GE),
					array('offtime', config::$nowtime, orm::OP_LT));
			$data['duration'] = (int)$orm->get_total($where, 'SUM(lasttime - aritime)');
			$data['start'] = $start;
		}
		$data['end']   = $data['start'] + $mtime;
		return $data;
	}
	
	//福州签约主播计算本月的开播时间
	private static function FZairTime($userid) {
		$sign  = model::load('live/sign_basic')->get_one($userid);
		$start = strtotime(date('Y-m-01',  config::$nowtime));
		$data['start'] = $sign['signtime'] > $start ? $sign['signtime'] : $start;
		$data['end']   = strtotime(date('Y-m-01 23:59:59', $start).' +1 month -1 day');
		$where = array('userid'=>$userid, array('offtime', $data['start'], orm::OP_GE),
				array('offtime', $data['end'], orm::OP_LT));
		$orm   = model::load('live/stuevent')->set_devision($userid);
		$data['duration'] = (int)$orm->get_total($where, 'SUM(lasttime - aritime)');
		return $data;
	}
}